package org.car.world.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.car.world.common.util.Page;
import org.car.world.core.entily.AdminTable;
import org.car.world.core.entily.IssueTable;
import org.car.world.core.entily.PictureTable;
import org.car.world.service.PictureService;
import org.car.world.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Administrator
 * @ClassName: PictureController
 * @Description: 资讯管理
 * @date Nov 15, 2018
 */
@Controller
@RequestMapping("admin")
public class PictureController {

    @Autowired
    TypeService typeservice;

    @Autowired
    PictureService pictureService;

    /**
     * @Title: dopictureadd_init @Description: 访问添加的初始化方法 @param @return 参数 @return
     * String 返回类型 @throws
     */
    @RequestMapping("dopictureadd")
    public String dopictureadd_init(Model mo) {
        mo.addAttribute("typelist", typeservice.selectAllidentify("TYPE_PICTURE"));
        return "picture_add";
    }

    /**
     * @param @return 参数 @return String 返回类型 @throws
     * @Title: dopictureadd_init
     * @Description: 访问列表方法
     */
    @RequestMapping("dopicturelist")
    public String dopicturelist_method(@RequestParam(value = "form", required = false, defaultValue = "1") Integer form,
                                       @RequestParam(value = "pictype", required = false, defaultValue = "0") Integer pictype, Model mo) {

        Page<PictureTable> Page = new Page<PictureTable>();
        Page.setCurrentPage(form);
        Page.setPageSize(2);
        Page.setTotalRow(pictureService.count(pictype));
        Page.setPageList(pictureService.selectLimit(Page.getCurrentPage(), Page.getPageSize(), pictype));
        mo.addAttribute("Page", Page);
        mo.addAttribute("pictype", pictype);
        mo.addAttribute("typelist", typeservice.selectAllidentify("TYPE_PICTURE"));
        return "picture_list";
    }

    /**
     * @param @return 参数 @return String 返回类型 @throws
     * @Title: dopictureadd_init
     * @Description: 添加, 更新方法
     */
    @RequestMapping("pictureaddAndpictureupdate")
    public String pictureadddata_method(PictureTable pic, Model mo, MultipartFile[] a_logoPicPath,
                                        HttpServletRequest request, HttpServletResponse response, HttpSession session, Integer sta)
            throws IllegalStateException, IOException {
        try {
            int flag = 0;
            String path = null;// 文件路径
            String Fpath = "templates\\upload\\";
            String fileName = "";
            String picrtu = "";
            String fu = "";
            for (int i = 0; i < a_logoPicPath.length; i++) {
                if (!a_logoPicPath[i].isEmpty()) {
                    //fu += "ㄓ";
                    fu += ",";
                    fileName = a_logoPicPath[i].getOriginalFilename();
                    System.err.println("上传的文件原名称:" + fileName);
                    String type = null;// 文件类型
                    type = fileName.indexOf(".") != -1
                            ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length())
                            : null;
                    if (type != null) {// 判断文件类型是否为空
                        if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase())
                                || "JPG".equals(type.toUpperCase())) {
                            // 项目在容器中实际发布运行的根路径
                            String realPath = request.getSession().getServletContext().getRealPath("/");
                            // 自定义的文件名称
                            String trueFileName = fileName;
                            // 设置存放图片文件的路径
                            path = realPath + Fpath + trueFileName;
                            System.err.println("存放图片文件的路径:" + path);
                            // 转存文件到指定的路径
                            a_logoPicPath[i].transferTo(new File(path));
                            System.err.println("文件成功上传到指定目录下");
                        }

                    } else {
                        System.out.println("不是我们想要的文件类型,请按要求重新上传");

                    }
                }

                // 使用特殊符号拼接
                picrtu += fu + Fpath + fileName;
            }

            pic.setInformationPicture(picrtu);

            // 1==添加方法
            if (sta == 1) {
                // 获得当前登录用户
                AdminTable adminTable = (AdminTable) session.getAttribute("Admin");
                pic.setInformationCreate(adminTable.getAdminId());
                flag = pictureService.insert(pic);
                if (flag > 0) {
                    return "dopicturelist";
                } else {
                    return "dopictureadd";
                }
                // 1==更新方法
            } else if (sta == 2) {

                flag = pictureService.updateByPrimaryKey(pic);
                if (flag > 0) {
                    return "dopicturelist";
                } else {
                    return "dopictureup";
                }

            }

            return "dopicturelist";

        } finally {
            System.err.println("添加文件方法执行完毕！");
        }

    }

    /**
     * @Title: dopictureadd_init
     * @Description: 删除 @param @return 参数 @return String 返回类型 @throws
     */
    @RequestMapping("dopicturedel")
    @ResponseBody
    public boolean dopictureadd_method(Integer picid) {
        int flage = pictureService.deleteByPrimaryKey(picid);
        if (flage > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param @return 参数 @return String 返回类型 @throws
     * @Title: dopictureadd_init
     * @Description: 查看信息方法以及访问信息方法
     */
    @SuppressWarnings("unlikely-arg-type")
    @RequestMapping("dopictureshow")
    public String dopictureinfo(Integer picid, Integer sta, Model mo) {
        List<String> datalist = new ArrayList<String>();
        String[] arrayDa;
        @SuppressWarnings("unused")
        String[] spdata = null;
        PictureTable pictureTable = pictureService.selectByPrimaryKey(picid);

        if (!"".equals(pictureTable)) {

            arrayDa = pictureTable.getInformationPicture().split("ㄓ");

            for (int i = 0; i < arrayDa.length; i++) {

                if (!"".equals(arrayDa[i]) && arrayDa[i] != "") {

                    datalist.add(arrayDa[i]);
                }
                continue;
            }

            if (datalist.size() > 0 || datalist.size() >= 0) {
                mo.addAttribute("imglist", datalist);
                mo.addAttribute("PictureTable", pictureTable);
                mo.addAttribute("typelist", typeservice.selectAllidentify("TYPE_PICTURE"));

                // 1=访问查看信息方法
                if (sta == 1) {
                    return "picture_info";
                    // 2=访问更新数据方法
                } else if (sta == 2) {
                    return "picture_update";
                }

            }
        }

        return "dopicturelist";

    }

    /**
     * @Title: dopictureupdatepic_init @Description: 更新图片 @param @param
     * picid @param @return 参数 @return Boolean 返回类型 @throws
     */
    @RequestMapping("dopictureupdatepic")
    @ResponseBody
    public Boolean dopictureupdatepic_init(Integer picid) {

        int upflag = pictureService.updatePic(picid);
        if (upflag > 0) {
            return true;
        } else {
            return false;
        }

    }

}
